<?php

namespace app\admin\model;

use think\Model;
use app\admin\model\Otherconfig;
use  EasyWeChat\Factory;


class Song extends Model
{

    

    

    // 表名
    protected $name = 'song';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    
    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password'])) {
                if ($changed['password']) {
                    $salt = \fast\Random::alnum();
                    $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
                    $row->salt = $salt;
                } else {
                    unset($row->password);
                }
            }
        });
        self::beforeInsert(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password'])) {
                if ($changed['password']) {
                    $salt = \fast\Random::alnum();
                    $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
                    $row->salt = $salt;
                } else {
                    unset($row->password);
                }
            }
        });
       
       
    }
    
    public function getStatusList()
    {
        return ['禁止登录','正常'];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }
    /**
     * 告诉师傅
     * Summary of sendMessage
     * @param mixed $user_id
     * @param mixed $orderId
     * @return array|object|string|\EasyWeChat\Kernel\Support\Collection|\Psr\Http\Message\ResponseInterface
     */
    public static function sendMessage($user_id,$orderId) {
       
            return self::common( $user_id,$orderId);
       
       
       
    }

    public static function common($user,$orderId,$statusText = '已分配' )
    {
        $siteConfig = Otherconfig::find(1);
        $config = [
            'app_id' => $siteConfig['app_id'],
            'secret' =>  $siteConfig['secret']
        ];
      
        $app = Factory::miniProgram($config);
        $result = $app->subscribe_message->send([
            'touser' =>   $user ,
            'template_id' => '3m5WnTYt3E-07Y3gqMydPsl3zaPse2gnao4WEHq6WjY',
            'page' => '',
            'data' => [
                'character_string4' => ['value' => $orderId],
                'thing3' => ['value' => '兴襄山泉' ],
                'thing8' => ['value' => '兴襄山泉'],
                'phrase14' => ['value' => $statusText]
            ],
        ]);
       
      
        return $result;
     
    }



}
